package com.yun.baymax.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

import com.yun.baymax.R;
import com.yun.baymax.activity.PublishActivity;
import com.yun.baymax.adapter.DiscoverAdapter;
import com.yun.baymax.bean.adapter.Trends;
import com.yun.lib.fragment.BaseFragment;
import com.yun.lib.utils.L;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class DiscoverFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.fab)
    FloatingActionButton fab;

    Context context;
    private DiscoverAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }

    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_discover;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState, View view, LayoutInflater inflater) {
        ButterKnife.bind(this, view);
        refreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.text_color_blue, R.color.voucher_color_red);
        refreshLayout.setNestedScrollingEnabled(true);
        refreshLayout.setOnRefreshListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        adapter = new DiscoverAdapter(getContext());
        mRecyclerView.setAdapter(adapter);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context, PublishActivity.class));
            }
        });

    }

    @Override
    protected void loadData() {
        refreshLayout.setRefreshing(true);
        BmobQuery<Trends> query = new BmobQuery<Trends>();
        query.setLimit(20);
        query.order("-createdAt");
        query.include("author");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(new FindListener<Trends>() {
            @Override
            public void done(List<Trends> object, BmobException e) {
                refreshLayout.setRefreshing(false);
                if (e == null) {
                    adapter.setDatas(object);
                } else {
                    L.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    @Override
    public void onResume() {
        super.onResume();
        context = getContext();
    }
}
